/**
 * The controller class for the introduction window
 * @author Team Love is Like Sand
 * @version 1.0
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * Controller for intro window
 * @author kushagramansingh
 * @version 1.0
 */
public class IntroController {
	
	
	/**
	 * Player that might be loaded
	 */
	private Player player;
	
	/**
	 * Universe that might be loaded
	 */
	private Universe universe;

	/**
	 * Checks if it can load window
	 * @param window that will be manipulated
	 * @return canLoad If game is loaded
	 */
	public boolean canLoad(IntroWindow window){
		String current = new File(".").getAbsolutePath();
		current = current.substring(0, current.length() - 1);
		File f = new File(current + "save.ser");
		if(f.exists()){
			FileInputStream fileIn = null;
			ObjectInputStream in = null; 
			
			try{
				fileIn = new FileInputStream(f);
				in = new ObjectInputStream(fileIn);
				player = (Player) in.readObject();
				universe = (Universe) in.readObject();
				window.setLoaded(true);
				
				return true;
			}
			catch(IOException e){
				e.getStackTrace();
			}
			catch(ClassNotFoundException t){
				t.printStackTrace();
			}
			finally{
				try{
					fileIn.close();
					in.close();
				}
				catch(IOException e){
					e.getStackTrace();
				}
			}
		}
		else{
			JOptionPane.showMessageDialog(new JFrame(), "No save game file found!");
		}
		return false;
	}
	
	/**
	 * Returns the Player
	 * @return player
	 */
	public Player getPlayer(){
		return player;
	}
	
	/**
	 * Returns the Universe
	 * @return universe
	 */
	public Universe getUniverse(){
		return universe;
	}
}
